<?php 
class CFaqController extends CController{

	protected $m_arrobjUsers;
	protected $m_objUser;
	protected $m_objFaq;
	
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'view_faqs':
				$this->handleViewFaqs();
				break;
				
			case 'add_edit_faq':
				$this->handleAddEditFaq();
				break;
							
			case 'insert_update_faq':
					$this->handleInsertUpdateFaq();
				break;
			case 'update_faq_action':
				$this->handleUpdateFaqAction();
				break;
			
			case 'view_faq_in_Detail':
					$this->handleFaqInDetail();
			break;
					
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewFaqs() {
		$this->m_arrobjFaqs = CFaqs::fetchFaqs( $this->m_objDatabase );	
		$this->displayFaqs();
	}
	
	function handleAddEditFaq() {
		include( "Liabraries/fckeditor/fckeditor.php");
	
		$this->m_objFCKeditor = new FCKeditor('faq[answer]') ;
		$this->m_objFCKeditor->BasePath = SITE_PATH . '/Liabraries/fckeditor/';
		$this->m_objFCKeditor->Width  = '100%';
		$this->m_objFCKeditor->Height = '500';
	
		$this->m_objFaq = new CFaq();
		
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objFaq = CFaqs::fetchFaqById( $_REQUEST['id'], $this->m_objDatabase );
			$this->m_objFCKeditor->Value  = $this->m_objFaq->getAnswer();
		}
		
	
		$this->displayAddEditFaq();
	}
	
	
	function handleInsertUpdateFaq() {
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'faq' ) );
		
				$this->m_objFaq = CPages::setValues( 'CFaq', $arrmixFieldsValues );
				$this->m_objFaq->setUserId($_SESSION['udeal.in']['login_user_id']);
				$this->m_objFaq->setCreatedOn(date('Y-m-d'));
				/* if( false == $this->m_objFaq->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				} */
				
				if( false == is_null( $this->m_objFaq->getId() ) && '' != trim( $this->m_objFaq->getId() ) ) {
				
					if( false == $this->m_objFaq->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update FAQ.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'FAQ update successfully. ' );
					}
				} else {
					
					if( false == $this->m_objFaq->insert( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create faq.' );
					} else {
						//display( $this->m_objFaq );die;
						$this->m_objErrorMessage->setSuccessMessage( 'FAQ created successfully. ' );
					}
				}
				redirect( '/admin/faq' );
		}
 		redirect( '/admin/faq/add_edit_faq' );
	}

	function handleUpdateFaqAction() {
	
		switch( true ) {
			default:
	
				$strAction = $this->m_objData->getPostData( array( 'faq', 'action' ) );
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
	
				$arrintFaqIds = $this->m_objData->getPostData( array( 'faq', 'id' ) );
				if( false == valArr( $arrintFaqIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
	
				$this->m_objFaq = new CFaq();
				if( false == valObj( $this->m_objFaq, 'CFaq' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
	
				if( 'delete' == $strAction ) {
					if( true == $this->m_objFaq->deleteFaqsByIds( $arrintFaqIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objFaq->updateFaqsActionByIds( $strAction, $arrintFaqIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
					
		}
		redirect( '/admin/faq' );
	}
	
	
	function handleFaqInDetail() {
		$intFaqId = $_REQUEST['id'];
		$this->m_objFaq = CFaqs::fetchFaqById( $intFaqId, $this->m_objDatabase );
	
		$this->displayFaqInDetail();
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayAddEditFaq() {
		$this->display();
	
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'faq', $this->m_objFaq );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/faq/view_add_edit_faq.tpl' );
	}
	
	function displayFaqs() {
		$this->display();

		$this->m_objSmarty->assign( 'faqs', $this->m_arrobjFaqs );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/faq/view_faqs.tpl');

	}
	
	function displayFaqInDetail() {
		$this->display();
	
		$this->m_objSmarty->assign( 'faq', $this->m_objFaq );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/faq/view_faq_in_detail.tpl');
	
	}
	
	function display() {
	
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'faq' );
	}

}
?>